package com.lidong.framework.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.lidong.framework.model.ResultMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lidong.framework.web.editor.DatePropertyEditor;
import com.lidong.framework.web.editor.DoublePropertyEditor;
import com.lidong.framework.web.editor.FloatPropertyEditor;
import com.lidong.framework.web.editor.IntPropertyEditor;
import com.lidong.framework.web.editor.LongPropertyEditor;

public class BaseController {
    private final static Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResultMessage handleException(Exception ex) {
		if(ex != null){
		    LOGGER.error("error! ", ex);
			return ResultMessage.error(ex.getMessage());
		}
		return ResultMessage.error();
	}
	
	/**
	 * 封装前台传入的String类型时间
	 * @param request
	 * @param binder
	 * @throws Exception
	 */
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new DatePropertyEditor());
		binder.registerCustomEditor(int.class, new IntPropertyEditor());
		binder.registerCustomEditor(long.class, new LongPropertyEditor());
		binder.registerCustomEditor(double.class, new DoublePropertyEditor());
		binder.registerCustomEditor(float.class, new FloatPropertyEditor());
	}
}
